﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class areaController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /area/

        public ActionResult Index()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string area = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.AREAs
                .Where(a => a.IdArea >= 1);
            if (!String.IsNullOrEmpty(area))
            {
                query = query.Where(a => (a.Nombre).ToUpper().Contains(area.ToUpper()));
            }
            var result = (from a in query.OrderBy(a => a.Nombre).Skip(start).Take(limit).ToList()
                          select new
                          {
                              IdArea = a.IdArea,
                              NomArea = a.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult IndexQR()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string area = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.AREAs
                .Where(a => a.IdArea >= 1);
            if (!String.IsNullOrEmpty(area))
            {
                query = query.Where(a => (a.Nombre).ToUpper().Contains(area.ToUpper()));
            }
            var result = (from a in query.OrderBy(a => a.Nombre).Skip(start).Take(limit).ToList()
                          select new
                          {
                              Id = a.IdArea,
                              Nombre = a.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult IndexQRById(int id)
        {

            var query = entidades.AREAs
                .Where(a => a.IdArea == id);

            var result = (from a in query.OrderBy(a => a.Nombre).ToList()
                          select new
                          {
                              Id = a.IdArea,
                              Nombre = a.Nombre,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /area/organigrama

        public ActionResult Organigrama()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);
            int idarea = 1;
            string area = Convert.ToString(Request.QueryString["node"]);

            var query = entidades.AREAs
                .Where(a => a.IdArea >= 1);

            if (!String.IsNullOrEmpty(area))
            {
                idarea = Convert.ToInt32(area);
                //query = query.Where(a => (a.Nombre).ToUpper().Contains(area.ToUpper()));
            }

            var result = (from a in query.OrderBy(a => a.Nombre)/*.Skip(start).Take(limit).*/.ToList()
                          //where a.Nivel == 1
                          where a.Dependencia == idarea
                          
                          select new
                          {
                              text = a.Nombre,
                              id = a.IdArea,
                              nivel = a.Nivel,
                              expanded = true
                          }).ToList();
            //var ww = result
            //return Json(entidades.Radendasporcontrato(1),JsonRequestBehavior.AllowGet);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /area/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /area/Create

        public ActionResult Create()
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO", Value = "false" });
            siono.Add(new SelectListItem { Text = "SI", Value = "true" });

            ViewData["Administracion"] = new SelectList(siono.AsEnumerable(), "value", "text", false);
            ViewData["ControlaProyectos"] = new SelectList(siono.AsEnumerable(), "value", "text", false);

            List<SelectListItem> TipoArea = new List<SelectListItem>();
            TipoArea.Add(new SelectListItem { Text = "General", Value = "general" });
            TipoArea.Add(new SelectListItem { Text = "Direccion", Value = "direccion" });
            TipoArea.Add(new SelectListItem { Text = "Sub Area", Value = "subarea" });

            ViewData["TipoArea"] = new SelectList(TipoArea.AsEnumerable(), "value", "text", "subarea");
            return View();
        } 

        //
        // POST: /area/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, AREA AREA)
        {
            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO", Value = "false" });
            siono.Add(new SelectListItem { Text = "SI", Value = "true" });

            ViewData["Administracion"] = new SelectList(siono.AsEnumerable(), "value", "text");
            ViewData["ControlaProyectos"] = new SelectList(siono.AsEnumerable(), "value", "text");

            List<SelectListItem> TipoArea = new List<SelectListItem>();
            TipoArea.Add(new SelectListItem { Text = "General", Value = "general" });
            TipoArea.Add(new SelectListItem { Text = "Direccion", Value = "direccion" });
            TipoArea.Add(new SelectListItem { Text = "Sub Area", Value = "subarea" });

            ViewData["TipoArea"] = new SelectList(TipoArea.AsEnumerable(), "value", "text");
            
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    var carea = new AREA();
                    TryUpdateModel(carea, collection.ToValueProvider());
                    //entidades.Rdesactivarcontratoadenda(ccontrato.IdPersonal,ccontrato.fechainicio);
                    entidades.AddToAREAs(carea);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /area/Edit/5
 
        public ActionResult Edit(int id)
        {
            var earea = entidades.AREAs.Single(ar => ar.IdArea == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO", Value = "false" });
            siono.Add(new SelectListItem { Text = "SI", Value = "true" });

            ViewData["Administracion"] = new SelectList(siono.AsEnumerable(), "value", "text", earea.Administracion);
            ViewData["ControlaProyectos"] = new SelectList(siono.AsEnumerable(), "value", "text", earea.ControlaProyectos);

            List<SelectListItem> TipoArea = new List<SelectListItem>();
            TipoArea.Add(new SelectListItem { Text = "General", Value = "general" });
            TipoArea.Add(new SelectListItem { Text = "Direccion", Value = "direccion" });
            TipoArea.Add(new SelectListItem { Text = "Sub Area", Value = "subarea" });

            ViewData["TipoArea"] = new SelectList(TipoArea.AsEnumerable(), "value", "text", earea.TipoArea);

            return View(earea);
        }

        //
        // POST: /area/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, AREA AREA)
        {
            var earea = entidades.AREAs.Single(ar => ar.IdArea == id);

            List<SelectListItem> siono = new List<SelectListItem>();
            siono.Add(new SelectListItem { Text = "NO", Value = "false" });
            siono.Add(new SelectListItem { Text = "SI", Value = "true" });

            ViewData["Administracion"] = new SelectList(siono.AsEnumerable(), "value", "text");
            ViewData["ControlaProyectos"] = new SelectList(siono.AsEnumerable(), "value", "text");

            List<SelectListItem> TipoArea = new List<SelectListItem>();
            TipoArea.Add(new SelectListItem { Text = "General", Value = "general" });
            TipoArea.Add(new SelectListItem { Text = "Direccion", Value = "direccion" });
            TipoArea.Add(new SelectListItem { Text = "Sub Area", Value = "subarea" });

            ViewData["TipoArea"] = new SelectList(TipoArea.AsEnumerable(), "value", "text");

            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    TryUpdateModel(earea);
                    entidades.SaveChanges();
                }
                return View(earea);
            }
            catch
            {
                return View(earea);
            }
        }
        //CambiarParent
        //[HttpPost]
        public ActionResult cambiarparent(int id, FormCollection collection)
        {
            var earea = entidades.AREAs.Single(ar => ar.IdArea == id);
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    TryUpdateModel(earea);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return Content("ERROR");
            }
            catch
            {
                return Content("ERROR");
            }
        }
        //
        // GET: /area/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /area/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
